package com.njy.decorator;

import com.njy.pojo.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
public class AsyncDecorator implements TaskDecorator {
    private final static Logger LOGGER = LoggerFactory.getLogger(AsyncDecorator.class);

    @Override
    public Runnable decorate(Runnable runnable) {
        Map<String, String> contextMap = MDC.getCopyOfContextMap();
        return () -> {
           // LOGGER.info("装饰器模式->将MDC放入子线程中"+contextMap.get(Constant.TRACE_ID));
            MDC.setContextMap(contextMap);
            runnable.run();
        };
    }
}
